package youcheng.work.view_zhexian;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MySqliteopenhelper extends SQLiteOpenHelper{

    private static final String db_name = "MySqlite.db";



    public MySqliteopenhelper(@Nullable Context context) {
        super(context, db_name, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table user(id int primary key ,  name varchar(32), password varchar(32))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public long register(User user){
        SQLiteDatabase db= getWritableDatabase();//以读写的方法打卡数据库
        ContentValues cv= new ContentValues();//内容值 相当于容器

        cv.put("name", user.getUser_name());//往容器里放数据
        cv.put("password", user.getPass_word());
        if(user.getUser_name() == null|| user.getPass_word() == null){
            return -1;
        }

        return db.insert("user", null, cv);
    }

    public boolean login(String name, String pwd) {
        SQLiteDatabase db = getWritableDatabase();//以读写的方法打卡数据库
        /* 通过用户名获取此行信息*/
        Cursor user_info = db.query("user", null, "name like ?", new String[]{name}, null, null, null);
        boolean result = false;

        if(user_info!=null){
            while(user_info.moveToNext()){
                String password = user_info.getString(1);
                result = password.equals(pwd);
                return  result;
            }
        }
        return false;
    }
}
